
<template>
  <div class="vuex-panel">
    <!-- 标题栏 -->
    <div class="panel-title">
      <p class="title-t">{{$t('cmlpanel.console_config')}}</p>
      <div class="search">
        <input class="search-input"
          type="text"
          v-model.trim="filter"
          placeholder="Filter State">
      </div>
      <!-- 路径 -->
      <ul class="path-list">
        <li class="path-item"
          @click="shuttle">state</li>
        <li v-for="(p, i) in path"
          :key="i"
          @click="shuttle(p)"
          class="path-item">{{p}}</li>
      </ul>
      <!-- 操作区域 -->
      <button class="btn btn-bl back"
        title="back"
        :disabled='path.length === 0'
        @click="lastStep"><i class="platformicon icon_fanhui"></i></button>
      <button class="btn btn-bl close"
        title="close"
        @click="close"><i class="platformicon icon_guanbimianban"></i></button>
    </div>
    <!-- 内容区域 -->
    <div class="body">
      <div class="last-step"
        v-show="path.length !== 0"
        @click="lastStep">
        <button class="btn"
          title="last step">
          <i class="platformicon icon_dakaimianban"></i>
        </button>
      </div>
      <ul class="list">
        <li class="item"
          v-for="(value, key, index) in filteredState"
          :key="index"
          :class="[(checkType(value) === 'Object' || checkType(value) === 'Array')?'clickable' : '']"
          @click.stop="select(key, checkType(value))">
          <span class="icon type-ref"
            :class="[(checkType(value) === 'Object' || checkType(value) === 'Array')?'platformicon icon_wenjianjia' : '']"></span>
          <span>{{key}}</span><span style="color: #fff;">:</span>
          <span v-if="checkType(value) === 'Object'"
            class="type-ref type-object">{{checkType(value)}}</span>
          <span v-else-if="checkType(value) === 'Array'"
            class="type-ref type-array">{{checkType(value)}}[{{value.length}}]</span>
          <span v-else
            class="value"
            :title="value"
            :class="[checkType(value)]">{{value}}</span>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import * as anov from 'anov-core'
import { mapMutations } from 'vuex'
import { searchDeepInObject, sortByKey } from './util'
// 控制台 zhu18-9-25
export default {
  name: 'VuexList',
  data() {
    return {
      states: null,
      // 对象的访问路径
      path: [],
      // 搜索
      filter: ''
    }
  },
  wacth: {},
  computed: {
    // 当前展示的数据
    currentShow() {
      let len = this.path.length,
        cache = this.states
      for (let i = 0; i < len; i++) {
        cache = cache[this.path[i]]
      }
      return cache
    },
    // 搜索
    filteredState() {
      const inspectedState = this.$store.state

      const getProcessedState = state => {
        if (!Array.isArray(state)) {
          return Object.keys(state).map(key => ({
            key,
            value: state[key]
          }))
        } else {
          return state
        }
      }

      const result = [].concat(...getProcessedState(inspectedState))

      let stateArr = sortByKey(
        result.filter(el =>
          searchDeepInObject(
            {
              [el.key]: el.value
            },
            this.filter
          )
        )
      )
      let cache = {}
      stateArr.map(s => {
        cache[s.key] = s.value
      })
      let len = this.path.length
      for (let i = 0; i < len; i++) {
        cache = cache[this.path[i]]
      }
      return cache
    }
  },
  mounted() {
    this.states = this.$store.state
  },
  methods: {
    ...mapMutations('cmlpanel', ['setConsolePanelVuex']),
    // 类型检查
    checkType(value) {
      let _toString = Object.prototype.toString
      return _toString.call(value).slice(8, -1)
    },
    // 选中某一个value
    select(key, type) {
      let keys = ['Object', 'Array']
      if (keys.includes(type)) {
        this.path.push(key)
      }
    },
    // 上一步
    lastStep() {
      this.path.pop()
    },
    // 在不同层级中穿梭
    shuttle(step) {
      this.path.splice(this.path.indexOf(step) + 1)
      if (typeof step === 'object') {
        this.path = []
      }
    },
    // 关闭
    close() {
      this.setConsolePanelVuex()
    }
  }
}
</script>

<style lang="scss" scoped>
@import '../../style/panel-var.scss';
@import '../../style/panel-mixin.scss';
$border-color: #a2d5d226;
.btn {
  height: 12px;
  padding: 0 10px;
  border: none;

  color: #c1fffb66;
  background: transparent;

  font-size: $fz-ssm;
  line-height: 1;
  &:hover {
    color: #c1fffbb3;
  }
  i {
    font-size: 12px;
  }
}
.btn-bl {
  border-left: 1px solid #c1fffb66;
}
button[disabled] {
  color: gray;

  cursor: not-allowed;
  &:hover {
    color: gray;
  }
}
.vuex-panel {
  flex: 1;
  overflow: auto;

  width: 100%;

  color: $text-color;

  font-family: Menlo, Monaco, 'Courier New', monospace;
  .panel-title {
    height: 24px;
    padding-left: 10px;
    box-sizing: border-box;
    // color: #999;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);

    background: #171515c4;

    @include flex(row, space-between);
    .title-t {
      margin-right: 10px;

      white-space: nowrap;
    }
    .search {
      height: 23px;
      padding-top: 2px;
      .search-input {
        height: 19px;
        padding: 0 5px;
        border: none;
        border-radius: 2px;

        color: #c1fffb;
        background-color: #272f2f;
        &:focus {
          background-color: #384343;
        }
      }
      input::-webkit-input-placeholder {
        color: gray;

        font-size: 12px;
      }
    }
    .path-list {
      margin-right: 10px;
      margin-left: auto;

      @include flex(row, flex-end);
      .path-item {
        opacity: 0.4;

        font-size: $fz-ssm;
        line-height: 1;

        transition: all ease 0.2s;

        cursor: pointer;
        &:after {
          content: '/';
        }
        &:nth-last-child(1) {
          opacity: 0.7;
          &:after {
            content: '';
          }
        }
        &:hover {
          opacity: 0.9;
        }
      }
    }
  }
  .body {
    overflow: auto;

    height: calc(100% - 24px);
  }
  .last-step {
    display: flex;
    align-items: center;

    position: sticky;
    top: 0;
    z-index: 1;

    height: 22px;
    padding-left: 10px;
    box-sizing: border-box;
    border-bottom: 1px solid #42424280;

    background-color: #161c1c;

    cursor: pointer;
    &:hover {
      background-color: #101313;
    }
    .btn {
      height: 14px;

      font-size: 14px;
      i {
        font-size: 14px;
      }
    }
  }
  .item {
    overflow: hidden;

    width: 100%;
    height: 20px;
    padding-left: 10px;
    border-bottom: 1px solid rgba(66, 66, 66, 0.5);

    color: #24e0f1;

    white-space: nowrap;
    text-overflow: ellipsis;
    &.clickable {
      cursor: pointer;
    }
    &:hover {
      background-color: #ffffff0d;
    }
    .type-ref {
      padding-left: 5px;

      color: #c1fffb;

      opacity: 0.8;

      &:hover {
        opacity: 1;
      }
    }
    .icon {
      display: inline-block;

      width: 12px;
      margin-right: 4px;
      padding: 0;

      font-size: $fz-ssm;
      line-height: 1;
      &.iconfont {
        cursor: pointer;
      }
    }
    .value {
      padding-left: 5px;
    }
    .Boolean,
    .Number {
      color: #f0a30d;
    }
    .String {
      color: #d65445;
      &:before {
        content: '"';
      }
      &:after {
        content: '"';
      }
    }
    .Undefined,
    .Null {
      color: #b5bec9;
    }
  }
}
</style>